package com.skill.shareskill.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.skill.shareskill.R;
import com.skill.shareskill.activity.InformationActivity;
import com.skill.shareskill.activity.LoginActivity;
import com.skill.shareskill.activity.SearchActivityActivity;
import com.skill.shareskill.bean.ActivityItem;
import com.skill.shareskill.bean.User;
import com.skill.shareskill.service.LoadDataThread;
import com.skill.shareskill.utils.GlideCircleTransform;
import com.skill.shareskill.utils.HttpUtils;

import java.util.List;

/**
 * Created by Administrator on 2018/1/31 0031.
 */

public class MyFragment extends Fragment {
    private ImageView iv_my_user_icon;
    private TextView tv_my_user_username;
    private TextView iv_pm;
    private Gson gson=new Gson();
    private User user;
    private Handler userHandle=new Handler(){
        public void handleMessage(Message message){
            String userJson= (String) message.obj;
            user=gson.fromJson(userJson,User.class);
            if(user!=null){
                tv_my_user_username.setText(user.getUserName());
                Glide.with(getContext())
                        .load(HttpUtils.BASE_PATH+user.getIcon())
                        .placeholder(R.drawable.pic1)
                        .transform(new GlideCircleTransform(getContext()))//实现圆形图片
                        .error(R.drawable.pic2)
                        .into(iv_my_user_icon);
            }
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment_my,container,false);
        tv_my_user_username=v.findViewById(R.id.tv_my_user_username);
        SharedPreferences sp = getActivity().getSharedPreferences("loginName",
                Context.MODE_PRIVATE);
        String name = sp.getString("name", "");
        tv_my_user_username.setText(name);
        Button logout= (Button) v.findViewById(R.id.btout);
        logout.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                SharedPreferences sp = getActivity().getSharedPreferences("loginName", 0);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("name",null);
                editor.putString("password","");
                editor.commit();
                Intent intent=new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });
        return v;

    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        iv_my_user_icon=view.findViewById(R.id.iv_my_user_icon);
        tv_my_user_username=view.findViewById(R.id.tv_my_user_username);
        String userUrl= HttpUtils.BASE_PATH+"user/queryUserById?id=2";
        LoadDataThread userLAT=new LoadDataThread(userHandle,userUrl);
        userLAT.start();


    }
}
